#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char const *argv[])
{
    //创建无名管道,通道0是读数据，1是写数据
    int pipefd[2];
    pipe(pipefd);
    __pid_t pid = fork();

    if(pid == 0){
        close(pipefd[1]);
        char buf;
        while (read(pipefd[0], &buf, 1) > 0)
        {
            write(STDOUT_FILENO, &buf, 1);
        }
        close(pipefd[0]);
        printf("我是子进程，数据接受完毕");
        exit(EXIT_SUCCESS);
        
    }else if(pid > 0)
    {
        close(pipefd[0]);
        //往管道中写入数据
        write(pipefd[1], argv[1], strlen(argv[1]));

        close(pipefd[1]);
        waitpid(pid, NULL, 0);
        printf("我已确认数据传输完毕\n");
        exit(EXIT_SUCCESS);
    }else
    {
        perror("fork");
        exit(EXIT_FAILURE);
    }
    
    

    return 0;
}
